import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 所有权限通用路由表
// 如首页和登录页和一些不用权限的公用页面
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: '首页',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }
]
// 异步挂载的路由
// 动态需要根据权限加载的路由表
export const asyncRouterMap = [
  {
    path: '/user',
    component: Layout,
    redirect: '/user/menu',
    name: 'User',
    meta: { title: '系统管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: '/user/menu',
        name: 'Menu',
        component: () => import('@/views/user/menu'),
        meta: { title: '菜单列表' }
      },
      {
        path: '/user/role',
        name: 'Role',
        component: () => import('@/views/user/role'),
        meta: { title: '角色列表' }
      },
      {
        path: '/user/admin',
        name: 'Admin',
        component: () => import('@/views/user/admin'),
        meta: { title: '管理员列表', keepAlive: true }
      },
      {
        path: '/user/log',
        name: 'Log',
        component: () => import('@/views/user/log'),
        meta: { title: '操作日志' }
      }
    ]
  },
  {
    path: '/stock',
    component: Layout,
    redirect: '/stock/goodsStock',
    name: 'Stock',
    meta: { title: '库存管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: '/stock/goodsStock',
        name: 'GoodsStock',
        component: () => import('@/views/stock/goodsStock'),
        meta: { title: '成品库存' }
      },
      {
        path: '/stock/materialStock',
        name: 'MaterialStock',
        component: () => import('@/views/stock/materialStock'),
        meta: { title: '物料库存' }
      }
    ]
  },
  {
    path: '/purchase',
    component: Layout,
    redirect: '/purchase/purchaseVehicle',
    name: 'Purchase',
    meta: { title: '采购管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: '/purchase/purchaseVehicle',
        name: 'PurchaseVehicle',
        component: () => import('@/views/purchase/purchaseVehicle'),
        meta: { title: '采购车' }
      },
      {
        path: '/purchase/purchaseList',
        name: 'PurchaseList',
        component: () => import('@/views/purchase/purchaseList'),
        meta: { title: '采购列表' }
      }
    ]
  },
  {
    path: '/warehouse',
    component: Layout,
    redirect: '/warehouse/storageList',
    name: 'Warehouse',
    meta: { title: '仓库位管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: '/warehouse/storageList',
        name: 'StorageList',
        component: () => import('@/views/warehouse/storageList'),
        meta: { title: '仓库列表' }
      },
      {
        path: '/warehouse/positionList',
        name: 'PositionList',
        component: () => import('@/views/warehouse/positionList'),
        meta: { title: '仓位管理' }
      },
      {
        path: '/warehouse/shelves',
        name: 'Shelves',
        component: () => import('@/views/warehouse/shelves'),
        meta: { title: '货架列表' }
      },
      {
        path: '/warehouse/storageArea',
        name: 'StorageArea',
        component: () => import('@/views/warehouse/storageArea'),
        meta: { title: '分区列表' }
      },
      {
        path: '/warehouse/storageSpace',
        name: 'StorageSpace',
        component: () => import('@/views/warehouse/storageSpace'),
        meta: { title: '分仓列表' }
      }
    ]
  },
  {
    path: '/store',
    component: Layout,
    redirect: '/store/storeList',
    name: 'Store',
    meta: { title: '店铺管理', icon: 'store' },
    children: [
      {
        path: '/store/storeList',
        name: 'StoreList',
        component: () => import('@/views/store'),
        meta: { title: '店铺列表' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// 清空一下之前的路由在进行添加
router.selfAddRoutes = function(params) {
  router.matcher = new Router().matcher
  router.addRoutes(params)
}

export default router
